package cn.itsource.org.service.impl;

import cn.itsource.basic.exception.CustomException;
import cn.itsource.basic.utils.AjaxResult;
import cn.itsource.basic.utils.BaiduAuditUtils;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.domain.ShopAuditLog;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopAuditLogMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.service.IShopService;
import cn.itsource.basic.base.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhi
 * @since 2022-07-05
 */
/**
 * 1.校验空参数
 *      2.校验店铺时候存在
 *      3.员工是否存在
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private  JavaMailSender javaMailSender;
    @Override
    public void settlement(Shop shop) {

        //判断店铺名是否为空
        if (StringUtils.isEmpty(shop.getName())){
            throw new CustomException("店铺名不可为空");
        }
        //百度AI  判断名字合不合法  不合法为false  为了能进打印语句 使用用非（!）
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new CustomException("店铺名不合法，请重新输入");
        }
        //判断店铺名是否存在
        Shop shop1 = shopMapper.queryByName(shop.getName());
        if(shop1 != null){
            throw new CustomException("店铺名已存在");
        }
        //判断员工是否存在
        Employee employee = employeeMapper.queryByUserName(shop.getAdmin().getUsername());
        if(employee != null){
            throw new CustomException("员工已注册");
        }
        //保存员工
        employeeMapper.insert(shop.getAdmin());
        //保存店铺
        shopMapper.insert(shop);
        //更新员工店铺ID
        shop.getAdmin().setShopId(shop.getId());
        employeeMapper.update(shop.getAdmin());
    }

    @Override//审核通过的情况
    public void auditPass(ShopAuditLog shopAuditLog) {
        //保存审核日志
        shopAuditLogMapper.insert(shopAuditLog);
        //修改店铺状态  根据id修改电普状态
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        //判断店铺存不存在
        if (shop == null){
            throw new CustomException("数据异常");
        }
        //修改状态
        shop.setState(2);
        shopMapper.update(shop);
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("1058169121@qq.com");
        //邮件主题
        mailMessage.setSubject("新型冠状病毒防护指南");
        //邮件内容
        mailMessage.setText("好好在家待着.....");
        //收件人
        mailMessage.setTo("2499146525@qq.com");

        javaMailSender.send(mailMessage);
    }


    @Override
    //驳回的情况
    public void auditReject(ShopAuditLog shopAuditLog) {
        //保存审核日志
        shopAuditLogMapper.insert(shopAuditLog);
        //修改店铺状态  根据id修改电普状态
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        //判断店铺存不存在
        if (shop == null){
            throw new CustomException("数据异常");
        }
        //修改状态
        shop.setState(4);
        shopMapper.update(shop);
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("1058169121@qq.com");
        //邮件主题
        mailMessage.setSubject("新型冠状病毒防护指南");
        //邮件内容
        mailMessage.setText("好好在家待着.....");
        //收件人
        mailMessage.setTo("2499146525@qq.com");

        javaMailSender.send(mailMessage);
    }

    @Override
    public List<Long> statistics() {
        return shopMapper.statistics();
    }

    @Override
    public void importExcel(List<Shop> shops) {
        shopMapper.importExcel(shops);
    }


}
